package com.smartHire.controller.User.Order_goods;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Address;
import com.smartHire.domain.Goods;
import com.smartHire.domain.Order;
import com.smartHire.service.GoodsService;
import com.smartHire.service.OrderService;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ShowOrderDetailServlet", value = "/ShowOrderDetailServlet")
public class ShowOrderDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id= request.getParameter("id");
        String status = request.getParameter("status");
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            OrderService orderService = (OrderService) ctx.getBean("orderService");
            UserService userService = (UserService) ctx.getBean("userService");
            GoodsService goodsService =(GoodsService) ctx.getBean("goodsService");

            Order order = new Order();

            List<Order> orders = null;

            System.out.println(status);
            if(status.equals("true"))
            {
                order.setOrder_Id(id);
                orders = orderService.queryOrdersByOrder_Id(order);

            }else {
                order.setOG_Id(Integer.parseInt(id));
                orders = orderService.queryOrderByOG_Id(order);
            }


            List list = new ArrayList();

            for(Order order1: orders)
            {

                Address address = new Address();
                address.setAddress_Id(order1.getAddress_Id());
                address = userService.queryAddressByAddressId(address);

                Goods goods = new Goods();
                goods.setGood_id(order1.getGood_id());
                goods = goodsService.queryGoodsByID(goods);


                list.add(order1);
                list.add(address);
                list.add(goods);
            }

            response.setCharacterEncoding("utf-8");
            String jsonStr = JSONObject.toJSONString(list);
            response.getWriter().write(jsonStr);

        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
